package com.atguigu.auth.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.auth.mapper.WechatMenuMapper;
import com.atguigu.auth.service.WechatMenuService;
import com.atguigu.model.wechat.Menu;
import com.atguigu.vo.wechat.MenuVo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 12646
* @description 针对表【wechat_menu(菜单)】的数据库操作Service实现
* @createDate 2023-11-23 08:46:17
*/
@Service
public class WechatMenuServiceImpl extends ServiceImpl<WechatMenuMapper, Menu>
    implements WechatMenuService{
  @Autowired
  private WechatMenuMapper menuMapper;
  @Autowired
  private WxMpService wxMpService;
  @Override
  public List<MenuVo> findMenuInfo() {
    List<MenuVo> list = new ArrayList<>();
    List<Menu> menuList = menuMapper.selectList(null);
    List<Menu> oneMenuList = menuList.stream().filter(menu -> menu.getParentId().longValue() == 0).collect(Collectors.toList());
    for (Menu oneMenu : oneMenuList) {
      MenuVo oneMenuVo = new MenuVo();
      BeanUtils.copyProperties(oneMenu, oneMenuVo);
      
      List<Menu> twoMenuList = menuList.stream()
          .filter(menu -> menu.getParentId().longValue() == oneMenu.getId())
          .sorted(Comparator.comparing(Menu::getSort))
          .collect(Collectors.toList());
      List<MenuVo> children = new ArrayList<>();
      for (Menu twoMenu : twoMenuList) {
        MenuVo twoMenuVo = new MenuVo();
        BeanUtils.copyProperties(twoMenu, twoMenuVo);
        children.add(twoMenuVo);
      }
      oneMenuVo.setChildren(children);
      list.add(oneMenuVo);
    }
    return list;
  }
  
  @Override
  public void syncMenu() {
    List<MenuVo> menuVoList = this.findMenuInfo();
    //菜单
    JSONArray buttonList = new JSONArray();
    for(MenuVo oneMenuVo : menuVoList) {
      JSONObject one = new JSONObject();
      one.put("name", oneMenuVo.getName());
      if(CollectionUtils.isEmpty(oneMenuVo.getChildren())) {
        one.put("type", oneMenuVo.getType());
        // 改成9090对应的域名
        one.put("url", "http://jian2.free.idcfengye.com/#"+oneMenuVo.getUrl());
      } else {
        JSONArray subButton = new JSONArray();
        for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
          JSONObject view = new JSONObject();
          view.put("type", twoMenuVo.getType());
          if(twoMenuVo.getType().equals("view")) {
            view.put("name", twoMenuVo.getName());
            //H5页面地址,改成9090对应的域名
            view.put("url", "http://jian2.free.idcfengye.com#"+twoMenuVo.getUrl());
          } else {
            view.put("name", twoMenuVo.getName());
            view.put("key", twoMenuVo.getMeunKey());
          }
          subButton.add(view);
        }
        one.put("sub_button", subButton);
      }
      buttonList.add(one);
    }
    //菜单
    JSONObject button = new JSONObject();
    button.put("button", buttonList);
    try {
      wxMpService.getMenuService().menuCreate(button.toJSONString());
    } catch (WxErrorException e) {
      throw new RuntimeException(e);
    }
  }
  
  @SneakyThrows
  @Override
  public void removeMenu() {
    wxMpService.getMenuService().menuDelete();
  }
}




